/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.template.client;

import java.util.List;

import com.cleangwt.user.shared.Link;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * A {@link NavbarFixedContainer} with a preserved sidebar container.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public abstract class NavbarSidebarContainer extends NavbarFixedContainer {
  private class SidebarToggleHandler implements CloseHandler<Sidebar>, OpenHandler<Sidebar> {

    @Override
    public void onClose(CloseEvent<Sidebar> event) {
      closeSidebar();
    }

    @Override
    public void onOpen(OpenEvent<Sidebar> event) {
      openSidebar();
    }

  }

  private static final int ANIMATINO_DURATION = 250;
  private static final int SIDEBAR_MARGIN_TOP = 10;

  protected SimplePanel sidebarContainer = new SimplePanel();
  protected SidebarToggleHandler toggleHandler = new SidebarToggleHandler();

  protected List<Link> links;

  /**
   * Creates a navbar-sidebar page container with the given links.
   * 
   * @param navbarStyle the navbar's type style
   * @param links the links
   */
  public NavbarSidebarContainer(String navbarStyle, List<Link> links) {
    super(navbarStyle);
    this.links = links;
    getLayout().addWest(sidebarContainer, Sidebar.TOGGLE_WIDTH);
    sidebarContainer.getElement().getStyle().setMarginTop(SIDEBAR_MARGIN_TOP, Unit.PX);
    this.openSidebar();
  }

  protected void closeSidebar() {
    getLayout().setWidgetSize(sidebarContainer, Sidebar.TOGGLE_WIDTH);
    getLayout().animate(ANIMATINO_DURATION);
  }

  protected void openSidebar() {
    getLayout().setWidgetSize(sidebarContainer, Sidebar.VIEW_WIDTH);
    getLayout().animate(ANIMATINO_DURATION);
  }

}
